
macro(install_qt_file filename)
  get_filename_component(filename_path ${filename} PATH)
  install(FILES "${qt_path}/${filename}" DESTINATION "OpenView.app/Contents/${filename_path}")
endmacro()

# Find out where the root Qt path is
get_target_property(qt_path Qt5::Quick IMPORTED_LOCATION_DEBUG)
get_filename_component(qt_path ${qt_path} PATH)
get_filename_component(qt_path ${qt_path} PATH)
get_filename_component(qt_path ${qt_path} PATH)

if(APPLE)
  file(GLOB vtk_libs "${VTK_DIR}/lib/lib*.dylib")
  foreach(vtk_lib ${vtk_libs})
    get_filename_component(vtk_lib_file ${vtk_lib} NAME)
    install(FILES ${vtk_lib} DESTINATION "OpenView.app/Contents/Libraries")
    install(CODE "execute_process(COMMAND ${CMAKE_INSTALL_NAME_TOOL} -id  @executable_path/../Libraries/${vtk_lib_file} ${CMAKE_INSTALL_PREFIX}/OpenView.app/Contents/Libraries/${vtk_lib_file})")
    set(install_name_changes "${install_name_changes} -change ${vtk_lib} @executable_path/../Libraries/${vtk_lib_file}")
    list(APPEND libs_and_execs "${CMAKE_INSTALL_PREFIX}/OpenView.app/Contents/Libraries/${vtk_lib_file}")
  endforeach()

  install_qt_file("qml/QtQuick.2/libqtquick2plugin.dylib")
  install_qt_file("qml/QtQuick.2/qmldir")
  install_qt_file("qml/QtQuick.2/plugins.qmltypes")

  install_qt_file("qml/Qt/labs/folderlistmodel/libqmlfolderlistmodelplugin.dylib")
  install_qt_file("qml/Qt/labs/folderlistmodel/qmldir")
  install_qt_file("qml/Qt/labs/folderlistmodel/plugins.qmltypes")

  foreach(lib ${libs_and_execs})
    install(CODE "execute_process(COMMAND ${CMAKE_INSTALL_NAME_TOOL} ${install_name_changes} ${lib})")
  endforeach()

  # Use Qt's macdeployqt to fix up the bundle
  install(CODE "execute_process(COMMAND ${qt_path}/bin/macdeployqt ${CMAKE_INSTALL_PREFIX}/OpenView.app)")
else()
  file(GLOB vtk_libs "${VTK_DIR}/bin/*.dll")
  foreach(vtk_lib ${vtk_libs})
    install(FILES ${vtk_lib} DESTINATION "bin")
  endforeach()
  file(GLOB qt_libs "${qt_path}/qtbase/bin/*.dll")
  foreach(qt_lib ${qt_libs})
    install(FILES ${qt_lib} DESTINATION "bin")
  endforeach()
  install(DIRECTORY ${qt_path}/qtbase/plugins DESTINATION "bin")
  install(DIRECTORY ${qt_path}/qtbase/qml DESTINATION "bin")
  install(FILES "${CMAKE_SOURCE_DIR}/qt.conf" DESTINATION "bin")
endif()

include(InstallRequiredSystemLibraries)

set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "OpenView information visualization application.")
set(CPACK_PACKAGE_VENDOR "Kitware, Inc.")
set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/README")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE")
set(CPACK_PACKAGE_VERSION_MAJOR "1")
set(CPACK_PACKAGE_VERSION_MINOR "0")
set(CPACK_PACKAGE_VERSION_PATCH "0")
set(CPACK_PACKAGE_INSTALL_DIRECTORY "OpenView")
if(WIN32 AND NOT UNIX)
  set(CPACK_NSIS_INSTALLED_ICON_NAME "\\\\OpenView.exe")
  set(CPACK_NSIS_DISPLAY_NAME "${CPACK_PACKAGE_INSTALL_DIRECTORY}")
  set(CPACK_NSIS_HELP_LINK "http://openview.kitware.com")
  set(CPACK_NSIS_URL_INFO_ABOUT "http://openview.kitware.com")
  set(CPACK_NSIS_CONTACT "jeff.baumes@kitware.com")
  set(CPACK_NSIS_MODIFY_PATH ON)
endif()
set(CPACK_PACKAGE_EXECUTABLES "OpenView" "OpenView")

include(CPack)
